package com.easywan.service;

import java.util.Date;
import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.easywan.dao.IBaseUsersDao;
import com.easywan.dao.IRToolsName2BaseUsersDao;
import com.easywan.exception.ExceptionSupport;
import com.easywan.exception.UsersExcetion;
import com.easywan.pojo.IResult;
import com.easywan.pojo.IResultSupport;
import com.easywan.vo.BaseUsers;
import com.easywan.vo.RToolsName2BaseUsers;
import com.easywan.vo.ToolsName;

import junit.framework.Assert;

public class BaseUsersService implements IBaseUsersService{
	private static Logger logger = Logger.getLogger(BaseUsersService.class);
	@Autowired
	private IBaseUsersDao usersDao;
	@Autowired
	private IRToolsName2BaseUsersDao toolsNameUsersDao;
	@Autowired
	private IToolsNameService nameService;
	
	@Transactional(value = "transactionManager",propagation = Propagation.REQUIRED,rollbackFor = Exception.class)
	public IResult register(String name, String pwd, String email,String mobile) {
		Assert.assertNotNull(name);
		Assert.assertNotNull(pwd);
		Assert.assertNotNull(email);
		BaseUsers users = usersDao.selectByName(name);
		if(users != null){
			throw ExceptionSupport.buildUsersExcetion(UsersExcetion.TYPE.NAME);
		}
		users = usersDao.selectByEmail(email);
		if(users != null){
			throw ExceptionSupport.buildUsersExcetion(UsersExcetion.TYPE.EMAIL);
		}
		BaseUsers tempUsers = new BaseUsers();
		
		tempUsers.setName(name);
		tempUsers.setPwd(pwd);
		tempUsers.setEmail(email);
		Date date = new Date();
		tempUsers.setCreatetime(date);
		tempUsers.setModifytime(date);
		tempUsers.setMobile(mobile);
		usersDao.insert(tempUsers);
		return IResultSupport.buildResult(IResult.SUCCESS_CODE,"注册成功");
	}

	public BaseUsers login(String loginUserName, String pwd) {
		
		BaseUsers tempUsers = new BaseUsers();
		tempUsers.setEmail(loginUserName);
		tempUsers.setPwd(pwd);
		BaseUsers users = usersDao.select(tempUsers);
		if(users == null) {
			tempUsers.setEmail("");
			tempUsers.setName(loginUserName);
			users = usersDao.select(tempUsers);
		}
		return users;
	}
	
	@Transactional(value = "transactionManager",propagation = Propagation.REQUIRED,rollbackFor = Exception.class)
	public IResult toolsAdd(String toolsName, Integer userId) {
		ToolsName toolsNameVo = nameService.getByName(toolsName);
		if(toolsNameVo == null){
			throw new RuntimeException("没有该工具！");
		}
		
		RToolsName2BaseUsers rtn2bu = new RToolsName2BaseUsers();
		rtn2bu.setUid(userId);
		rtn2bu.setToolsNameId(toolsNameVo.getId());
		logger.info(rtn2bu);
		{
			List<RToolsName2BaseUsers> temp = toolsNameUsersDao.selectBy(rtn2bu);
			if(null != temp && temp.size() > 0){
				logger.info("重复添加工具！！");
				throw new RuntimeException("重复添加工具！！");
			}
		}
		Date date = new Date();
		rtn2bu.setCreatetime(date);
		rtn2bu.setModifytime(date);
		int counter = toolsNameUsersDao.insert(rtn2bu);
		if(counter > 0){
			return IResultSupport.buildResult(IResult.SUCCESS_CODE, "添加工具成功");
		}
		return IResultSupport.buildResult(IResult.FAILT_CODE, "添加失败");
	}

	
	
}
